package com.nameless.controller.api;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.nameless.annotation.HasPermission;
import com.nameless.annotation.MyLog;
import com.nameless.entity.Menu;
import com.nameless.service.MenuService;
import com.nameless.vo.Result;
import com.nameless.vo.TreeSelect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author : 陈灰灰
 * @Date: 2023/02/26/15:33
 * @Description:
 */
@Api(value = "system/api/menu", tags = {"权限控制层"})
@RestController
@RequestMapping("system/api/menu")
public class ApiPermissionController {
    @Autowired
    private MenuService menuService;

    /**
     * @Author chh
     * 通过用户id查询权限
     * @return {@link Result}
     */
    @ApiOperation(value = "通过用户id查询权限", notes = "通过用户id查询权限")
    @RequestMapping("/selectMenuTree")
    @MyLog(title = "aaaa",businessType = 0)
    public Result getPertmissionByUserId(){
        List<Menu> menus = menuService.leftMenu();
        return new Result(200,"查询菜单栏成功",menus);
    }

    /**
     * @Author chh
     * 分配权限
     * @param roleId  角色id
     * @param menuids menuids
     * @return {@link Result}
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "roleId", value = "角色id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer[]", name = "menuids", value = "menuids", required = true)
    })
    @ApiOperation(value = "分配权限", notes = "分配权限")
    @RequestMapping("qr")
    public Result qr(Integer roleId,Integer[] menuids){
        System.out.println(Arrays.toString(menuids));
        Result qr = menuService.qr(roleId, menuids);
        return qr;
    }

    /**
     * @Author chh
     * 根据角色id查询权限列表
     * @param roleId 角色id
     * @return {@link Result}
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "roleId", value = "角色id", required = true)
    })
    @ApiOperation(value = "根据角色id查询权限列表", notes = "根据角色id查询权限列表")
    @RequestMapping("showList")
    public Result showList(Integer roleId){
      return menuService.findMenuByRoleId(roleId);
    }
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Menu", name = "menu", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "")
    @RequestMapping("insert")
    @HasPermission("system:menu:add")
    public Result insert(@RequestBody Menu menu){
        boolean save = menuService.save(menu);
        return new Result(200,"添加成功");
    }

    /**
     * @Author chh
     * 通过menuId修改权限
     * @param menu 菜单
     * @return {@link Result}
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Menu", name = "menu", value = "菜单", required = true)
    })
    @ApiOperation(value = "通过menuId修改权限", notes = "通过menuId修改权限")
    @RequestMapping("update")
    @HasPermission("system:menu:edit")
    public Result update(@RequestBody Menu menu){
        menuService.updateById(menu);
        return new Result(200,"修改成功");
    }

    /**
     * @Author chh
     * 批量删除
     * @param idList id列表
     * @return {@link Result}<{@link String}>
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "List<Integer>", name = "idList", value = "id列表", required = true)
    })
    @ApiOperation(value = "删除多个系统管理信息", notes = "批量删除", httpMethod = "DELETE")
    @DeleteMapping("delete")
    @HasPermission("system:menu:remove")
    @ApiImplicitParam(name = "idList",value = "需要删除id集合")
    public Result<String> delete(@RequestBody List<Integer> idList){
        boolean b = menuService.removeByIds(idList);
        return b?Result.success("删除成功"):Result.error("删除失败");
    }

    /**
     * @Author
     * 得到菜单
     * @param id id
     * @return {@link Result}
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "id", required = true)
    })
    @ApiOperation(value = "得到菜单", notes = "得到菜单")
    @RequestMapping("/{id}")
    public Result getMenus(@PathVariable Integer id){
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getMenuId,id);
        Menu menu = menuService.getOne(queryWrapper);
        return new Result(200,"根据菜单id查询成功",menu);
    }

    /**
     * @Author chh
     * 编辑菜单可用
     * @param menuId  菜单id
     * @param visible 可见
     * @return {@link Result}<{@link String}>
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "menuId", value = "菜单id", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "visible", value = "可见", required = true)
    })
    @ApiOperation(value = "启用禁用", notes = "编辑菜单可用", httpMethod = "PUT")
    @PutMapping("/{menuId}/{visible}")
    public Result<String> editMenuByAvailable(@PathVariable Integer menuId , @PathVariable Integer visible){
        LambdaUpdateWrapper<Menu> sysMenuWrapper = new LambdaUpdateWrapper<>();
        sysMenuWrapper.set(Menu::getVisible,visible);
        sysMenuWrapper.eq(Menu::getMenuId,menuId);
        boolean save = menuService.update(sysMenuWrapper);
        return save?Result.success("修改成功"):Result.error("修改失败");
    }

    /**
     * @Author chh
     * 动态获取所有权限菜单
     * @return {@link Result}<{@link List}<{@link Menu}>>
     */
    @ApiOperation(value = "动态获取所有权限菜单", notes = "动态获取所有权限菜单", httpMethod = "GET")
    @GetMapping("/all")
    public Result<List<Menu>> menuAll(){
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.notIn(Menu::getMenuType,"F");
        List<Menu> sysMenus = menuService.list(wrapper);
        return new Result<>(200,"加载全部菜单成功",sysMenus);
    }

    /**
     * @Author chh
     * 懒加载获取信息
     * @param id id
     * @return {@link Result}<{@link List}<{@link Menu}>>
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "id", required = true)
    })
    @ApiOperation(value = "懒加载获取信息", notes = "懒加载获取信息", httpMethod = "GET")
    @GetMapping("/pid/{id}")
    public Result<List<Menu>> lazyMenu(@PathVariable Integer id){
        List<Menu> allParent = menuService.findAllParent(id);
        return new Result<>(200,"加载父类成功",allParent);

    }

    /**
     * @Author chh
     * 动态获取所有权限菜单并分级
     * @return {@link Result}<{@link List}<{@link TreeSelect}>>
     */
    @ApiOperation(value = "动态获取所有权限菜单并分级", notes = "动态获取所有权限菜单并分级", httpMethod = "GET")
    @GetMapping("/allClass")
    public Result<List<TreeSelect>> menuAllWithClass(){
        List<TreeSelect> sysMenus = menuService.listWithClass();
        return new Result<>(200,"加载全部菜单并分级成功",sysMenus);
    }

}
